package com.hxzy.commons.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxzy.commons.CommonConstants;
import com.hxzy.commons.model.CmsCodeData;
import com.hxzy.commons.model.CmsCodeIndex;
import com.hxzy.commons.model.CmsCodedataId;
import com.hxzy.commons.service.ICacheService;
import com.hxzy.commons.util.DateUtil;
import com.hxzy.commons.util.JSONLibUtil;
import com.hxzy.core.base.action.BaseAction;
import com.hxzy.core.cache.DictCache;
import com.hxzy.core.page.entity.PageParams;
import com.hxzy.core.page.entity.Pagination;


/**
 * <p>
 * 类描述：字典数据维护
 * <p>
 * 创建人：Abdi
 * <p>
 * 创建时间：2013-11-5 下午10:28:40
 */
@Controller
@RequestMapping("/auth/dict")
@SuppressWarnings("unchecked")
public class DictManageAction extends BaseAction{
	@Autowired
	private ICacheService cacheService;
	
	/**
	 * 加载字典集列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/loadDictList.do")
	public String loadDictList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String codetype = request.getParameter("codetype");
		Pagination page = cacheService.loadDicttList(pageParams,codetype);

		model.addAttribute("page",page);
		model.addAttribute("codetype",codetype);
		return "app/module/dict/query-dict-list";
	}
	
	/**
	 * 选择一个字典集
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectOneDict.do")
	public String selectOneDict(HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("codetype", request.getParameter("codeytpe"));
		CmsCodeIndex codeindex = (CmsCodeIndex)cacheService.getUniqueObject(CmsCodeIndex.class, query);
		
		model.addAttribute("codeindex",codeindex);
		return "app/module/dict/update-one-dict";
	}
	
	/**
	 * 新增一个字典集
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneDict.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneDict(CmsCodeIndex codeindex,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("codetype", codeindex.getCodetype());
		Object existData = cacheService.getUniqueObject(CmsCodeIndex.class, query);
		if(null != existData){
			return "字典名称已存在！";
		}
		codeindex.setStatus(CommonConstants.YOUXIAOBJ_YX);
		codeindex.setStartdate(DateUtil.currentDateTime());
		cacheService.save(codeindex);
		DictCache cache = DictCache.getInstance();
		cache.addOneCodetype(codeindex);
		return "保存成功！";
	}
	
	/**
	 * 修改一个字典集
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneDict.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneDict(CmsCodeIndex codeindex,HttpServletRequest request,ModelMap model){
		codeindex.setModifytime(DateUtil.currentDateTime());
		codeindex.setStatus(1L);
		cacheService.save(codeindex);
		return "修改成功！";
	}
	
	/**
	 * 删除一个字典集
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneDict.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneDict(HttpServletRequest request,ModelMap model){
		String codetype = request.getParameter("codetype");
		cacheService.deleteMoreDictData(codetype);
		CmsCodeIndex codeIndex = new CmsCodeIndex();
		codeIndex.setCodetype(codetype);
		cacheService.deleteObject(codeIndex);
		DictCache cache = DictCache.getInstance();
		cache.delOneCodetype(codetype);
		return "删除成功！";
	}
	
	/**
	 * 获取字典集数据
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/treeData4Dict.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String treeData4Dict(HttpServletRequest request,ModelMap model){
		StringBuilder result = new StringBuilder();
		List<Map> resultList = cacheService.getData4Tree("");//暂时只生成一种树
		if(resultList != null && resultList.size() > 0 ){
			result.append("[").append("{\"id\":\"0\",\"name\":\"字典参数\",\"level\":\"0\",\"open\":\"true\"}");//设置默认根节点
			for(Map map : resultList){
				result.append(",").append(JSONLibUtil.Map2Json(map));
			}
			result.append("]");
		}
		System.out.println(result);
		return result.toString();
	}
	
	@RequestMapping(value="/addOneDictData.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneDictData(CmsCodeData codedata,HttpServletRequest request,ModelMap model){
		Object existData = cacheService.getOneDictData(codedata.getId().getCodetype(), codedata.getId().getCodevalue());
		if(null != existData){
			return "字典名称已存在！";
		}
		codedata.setStatus(CommonConstants.YOUXIAOBJ_YX);
		codedata.setStartdate(DateUtil.currentDateTime());
		cacheService.save(codedata);
		DictCache cache = DictCache.getInstance();
		cache.addOneCodeData(codedata);
		return "保存成功！";
	}
	
	@RequestMapping(value="/selectOneDictData.do")
	public String selectOneDictData(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String codetype = request.getParameter("codetype");
		String codevalue = request.getParameter("codevalue");
		CmsCodeData resultdata =(CmsCodeData)cacheService.getOneDictData(codetype, codevalue);
		model.addAttribute("codedata", resultdata);
		return "app/module/dict/update-one-dictdata";
	}
	
	@RequestMapping(value="/editOneDictData.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneDictData(CmsCodeData codedata,HttpServletRequest request,ModelMap model){
		cacheService.save(codedata);
		DictCache cache = DictCache.getInstance();
		cache.updateOneCodeData(codedata);
		return "修改成功！";
	}
	
	/**
	 * 删除一个字典集数据
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneDictData.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneDictData(HttpServletRequest request,ModelMap model){
		String codetype = request.getParameter("codetype");
		String codevalue = request.getParameter("codevalue");
		CmsCodedataId codedataId = new CmsCodedataId();
		codedataId.setCodetype(codetype);
		codedataId.setCodevalue(codevalue);
		CmsCodeData codedata = new CmsCodeData();
		codedata.setId(codedataId);
		cacheService.deleteObject(codedata);
		DictCache cache = DictCache.getInstance();
		cache.deleteOneCodeData(codedataId);
		return "删除成功！";
	}
	
	@RequestMapping(value="/loadDictDataList.do")
	public String loadDictDataList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String codetype = request.getParameter("codetype");
		String codevalue =request.getParameter("codevalue");
		CmsCodedataId id = new CmsCodedataId();
		id.setCodetype(codetype);
		id.setCodevalue(codevalue);
		Pagination page = cacheService.loadDictDataList(pageParams,id);

		model.addAttribute("page",page);
		model.addAttribute("codevalue",codevalue);
		return "app/module/dict/query-dictdata-list";
	}
}